const Ec = require('../epic');
const fs = require("fs");
const Io = require("./ai.fn.initialize.__.zero.directory");
const IoUt = require("./ai.fn.initialize.__.io.util");

const initModuleConfiguration = (parsed = {}) => {
    const module = parsed.module;
    const configuration = IoUt.ioConfiguration(parsed, module);
    configuration.artifactId = `zero-extension-${module}`;
    configuration.srcType = Symbol("MODULE");
    return configuration;
}
const initMod = async (configuration = {}) => {


    // 1. 先创建基本目录
    Ec.execute("----------- 目录创建 -----------");
    const baseDir = configuration.srcOut;
    const created = await Io.ioDPAStructure(baseDir, configuration);
    if (!(created.every(item => true === item))) {
        Ec.error(`目录初始化异常：${configuration.srcOut}`)
    }

    Ec.execute("----------- 文件生成 -----------");
    // 2. 每个项目的 pom.xml 文件初始化
    const type = configuration.srcType;
    const sourceTpl = Ec.ioRoot() + "/_template/" + type.description;
    const genPom = await Io.ioDPAPom(sourceTpl, configuration);
    if (!genPom) {
        Ec.error(`pom.xml 文件生成异常：${configuration.srcOut}`)
    }


    // 3. 数据库文件初始化
    Ec.execute("----------- 数据库初始化 -----------");
    const genDatabase = await Io.ioModuleDatabase(sourceTpl, configuration);
    if (!genDatabase) {
        Ec.error(`数据库文件生成异常：${configuration.srcOut}`)
    }


    // 4. 代码生成文件初始化
    Ec.execute("----------- 代码生成 -----------");
    const genModule = await Io.ioModuleConfiguration(sourceTpl, configuration);
    if (!genModule) {
        Ec.error(`配置文件生成异常：${configuration.srcOut}`)
    }
    const genSource = await Io.ioModuleSource(sourceTpl, configuration);
    if (!genSource) {
        Ec.error(`源代码文件生成异常：${configuration.srcOut}`)
    }

    // 5. 权限变更
    const genChmod = await IoUt.ioChmod(configuration.srcOut);
    if (!genChmod) {
        Ec.error(`权限变更异常：${configuration.srcOut}`)
    }
}

module.exports = {
    initMod,
    initModuleConfiguration,
}